package com.sample.wanandroidx.ui.home

import android.content.Intent
import android.widget.LinearLayout
import com.just.agentweb.AgentWeb
import com.sample.library.ext.startActivity
import com.sample.wanandroidx.app.App
import com.sample.wanandroidx.basic.BaseActivity
import com.sample.wanandroidx.databinding.ActivityStartBinding

////使用AppCompatActivity减少初始化时间
class StartActivity : BaseActivity<ActivityStartBinding>(ActivityStartBinding::inflate) {
    private var agentWeb: AgentWeb? = null
    override fun initialization() {
        if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0) {
            finish()
        } else {
            postDelayed(1000) {
                goToMainActivity()
            }
        }
        initAgentWeb()
    }

    //预初始化
    private fun initAgentWeb() {
        agentWeb =
            AgentWeb.with(this).setAgentWebParent(LinearLayout(this), LinearLayout.LayoutParams(-1, -1)).useDefaultIndicator()
                .createAgentWeb().ready().go("file:///android_asset/index.html")
    }

    private fun goToMainActivity() {
        when (App.user == null) {
            true -> {
                //                startActivity<LoginActivity>()
                startActivity<MainActivity>()
            }
            false -> {
                startActivity<MainActivity>()
            }
        }
        finish()
    }

    override fun onBackPressed() {
        super.onBackPressed()
        getHandler()?.removeCallbacksAndMessages(null)
    }

    override fun onDestroy() {
        super.onDestroy()
        agentWeb?.destroy()
    }
}